import 'package:flutter/material.dart';

import '../../common/models/common_meal_model.dart';
import '../tools/network/network_manager.dart';
import 'filter_provider.dart';

class YSHMealListProvider extends ChangeNotifier {

  // 1.定义要分享的数据
  List<YSHCommonMealModel> _mealList = [];
  // 2.重写get方法
  List<YSHCommonMealModel> get mealList {
    return _mealList;
  }

  List<YSHCommonMealModel> getFliteredMealList() {
    return _mealList.where((element) {
      if (_filterProvider != null && element.isGlutenFree != null) {
        if (_filterProvider!.isGlutenFree && !element.isGlutenFree!) {
          return false;
        }
      }
      if (_filterProvider != null && element.isLactoseFree != null) {
        if (_filterProvider!.isLactoseFree && !element.isLactoseFree!) {
          return false;
        }
      }
      if (_filterProvider != null && element.isVegetarian != null) {
        if (_filterProvider!.isVegetarian && !element.isVegetarian!) {
          return false;
        }
      }
      if (_filterProvider != null && element.isVegan != null) {
        if (_filterProvider!.isVegan && !element.isVegan!) {
          return false;
        }
      }
      return true;
    }).toList();
  }

  YSHFilterProvider? _filterProvider;
  void updateFilters(YSHFilterProvider filterProvider) {
    _filterProvider = filterProvider;
  }

  YSHMealListProvider() {
    NetworkManager.request('api/meal').then((value) {
      Map<String, dynamic> map = value as Map<String, dynamic>;
      List mealMapList = map['meal'];
      List<YSHCommonMealModel> modelList = [];
      for (var mealMap in mealMapList) {
        modelList.add(YSHCommonMealModel.fromMap(mealMap));
      }
      _mealList = modelList;
      notifyListeners();
    });
  }
}